package com.xiaozhen.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaozhen.blog.entity.Blog;
import com.xiaozhen.blog.entity.Comment;
import com.xiaozhen.blog.entity.Event;
import com.xiaozhen.blog.entity.User;
import com.xiaozhen.blog.mapper.BlogMapper;
import com.xiaozhen.blog.mapper.CommentMapper;
import com.xiaozhen.blog.mapper.EventMapper;
import com.xiaozhen.blog.mapper.UserMapper;
import com.xiaozhen.blog.service.BlogService;
import com.xiaozhen.blog.service.CommentService;
import com.xiaozhen.blog.service.EventService;
import com.xiaozhen.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @create 2023-05-24 15:38
 */
@Service
public class EventServiceImpl extends ServiceImpl<EventMapper, Event> implements EventService {
    @Autowired
    private EventMapper eventMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private BlogMapper blogMapper;


    // TODO 添加数据
    public void add(Event event) {
        eventMapper.insert(event);
    }

    /**
     * 获取通知消息列表
     *
     * @param messageType
     */
    @Override
    public List<Event> getEventsByMessageType(int messageType) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String userId = authentication.getName();
        // 1. 获取相关数据，查询 t_event
        LambdaQueryWrapper<Event> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Event::getUserId, userId); // 查找当前用户的 id
        wrapper.eq(Event::getEntityType, messageType); // 0 代表是评论信息通知 1 代表是回复信息通知
        List<Event> events = eventMapper.selectList(wrapper);

        if (messageType == 0) {
            for (Event event : events) {
                User user = userMapper.selectById(event.getEntityUserId());
                Comment comment = commentMapper.selectById(event.getEntityId());
                Blog blog = blogMapper.selectById(comment.getBlogId());
                event.setData("headerUrl", user.getHeaderUrl())
                        .setData("nickname", user.getNickname())
                        .setData("content", comment.getContent())
                        .setData("blogId", comment.getBlogId())
                        .setData("blogTitle", blog.getTitle())
                        .setData("createTime", comment.getCreateTime());
            }
        } else if (messageType == 1) {
            for (Event event : events) {
                User user = userMapper.selectById(event.getEntityUserId());
                Comment comment = commentMapper.selectById(event.getEntityId());
                Blog blog = blogMapper.selectById(comment.getBlogId());
                event.setData("headerUrl", user.getHeaderUrl())
                        .setData("nickname", user.getNickname())
                        .setData("content", comment.getContent())
                        .setData("blogId", comment.getBlogId())
                        .setData("blogTitle", blog.getTitle())
                        .setData("createTime", comment.getCreateTime());
            }
        }

        return events;
    }
}
